<?php

declare(strict_types=1);
namespace App\Service\AppService;

use App\Service\BaseService;
use App\Model\PssModel\Sysconfig;
use App\Service\Code\Normal;

/**
 * @author chenshubo
 * 获取指定缓存值
 */
class SystemCacheConfigService extends BaseService
{
    const SYSTEM_CACHE_CONFIG_SERVICE_KEY = 'SystemCacheConfigServiceKey:';
    const SYSTEM_CACHE_CONFIG_EXPIRE      = 86400;
    const SYSTEM_CACHE_CONFIG_RDB         = 10;
    /**
     * 获取系统指定配置缓存
     * @param string $code
     */
    public function getCacheInfoBySystemCode( string $code ) 
    {
        $redis  = $this->getRedisObj();
        $redis->select(self::SYSTEM_CACHE_CONFIG_RDB);
        $key    = self::SYSTEM_CACHE_CONFIG_SERVICE_KEY.$code;
        $rv     = $redis->get($key);
        if( empty($rv) ) {
            $info = Sysconfig::instance()->info(['code'=>$code]);
            if( empty($info) ) {
                return [];
            }
            $rv = $info['contents'];
            $redis->set($key,$rv,self::SYSTEM_CACHE_CONFIG_EXPIRE);
        }
        return @json_decode($rv,true);
    }
    
    /**
     * 检查字段是否正常
     * @param array $config
     * @param array $fields
     */
    public function checkAllowApiColumnsByFields( array $config = [] ,array $fields = [] ) 
    {
        foreach ( $fields as $checkField ) {
            if( !isset($config[$checkField]) || empty($config[$checkField]) ) {
                return Normal::returnCode(Normal::FAIL,"参数{$checkField}不能为空");
            }
        }
        return Normal::returnCode(Normal::SUC,"校验通过");
    }
}